package com.ddstation.onsitelaw.service;


import com.ddstation.onsitelaw.entity.DtInvsPrescriptionManualAuditEntity;
import com.ddstation.onsitelaw.entity.DtPrescriptionTemplateManageEntity;
import com.ddstation.onsitelaw.entity.MaInvsViolationMessageEntity;
import com.ddstation.onsitelaw.mapper.AuditPrescriptionMapper;
import com.ddstation.onsitelaw.mapper.DtInvsManageInstitutionSupervisorMapper;
import com.ddstation.onsitelaw.mapper.DtPrescriptionTemplateManageMapper;
import com.ddstation.onsitelaw.mapper.MaInvsViolationMessageMapper;
import com.ddstation.onsitelaw.model.AccountModel;
import com.ddstation.onsitelaw.model.AuditPrescriptionModel;
import com.ddstation.onsitelaw.model.TemplateModel;
import com.ddstation.onsitelaw.model.ViolationManageModel;
import com.ddstation.onsitelaw.utils.AliCloudOssUtil;
import com.ddstation.onsitelaw.utils.DateUtil;
import com.ddstation.onsitelaw.utils.PageResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.inject.Inject;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class AuditPrescriptionServiceImpl implements AuditPrescriptionService {
    @Inject
    private AuditPrescriptionMapper auditPrescriptionMapper;

    @Inject
    private MaInvsViolationMessageMapper maInvsViolationMessageMapper;

    @Inject
    private DtPrescriptionTemplateManageMapper dtPrescriptionTemplateManageMapper;

    @Inject
    private DtInvsManageInstitutionSupervisorMapper dtInvsManageInstitutionSupervisorMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertAuditPrescription(AuditPrescriptionModel auditPrescriptionModel) {
        DtInvsPrescriptionManualAuditEntity auditEntity = new DtInvsPrescriptionManualAuditEntity();
        BeanUtils.copyProperties(auditPrescriptionModel, auditEntity);
        auditPrescriptionMapper.insertSelective(auditEntity);
    }

    @Override
    @Transactional(readOnly = true)
    public List<AccountModel> selectPersonnel(HashMap paramMap) {
        return auditPrescriptionMapper.selectPersonnel(paramMap);
    }

    @Override
    @Transactional(readOnly = true)
    public PageResult selectNotAuditList(HashMap paramMap) {
        //查询ma_invs_violation_message，获取title
        List<MaInvsViolationMessageEntity> msgList = maInvsViolationMessageMapper.selectList();
        Map<String, String> msgTitleMap = msgList.stream().collect(Collectors.toMap(MaInvsViolationMessageEntity::getMsgId, MaInvsViolationMessageEntity::getTitle));

        List<AccountModel> accountList = dtInvsManageInstitutionSupervisorMapper.selectAllSupervisors();
        Map<String, String> accountMap = accountList.stream().collect(Collectors.toMap(AccountModel::getLoginUser, AccountModel::getName));

        PageHelper.startPage(Integer.parseInt(paramMap.get("pageNum").toString()), Integer.parseInt(paramMap.get("pageSize").toString()));
        List<ViolationManageModel> list = auditPrescriptionMapper.selectNotAuditList(paramMap);
        PageInfo<ViolationManageModel> info = new PageInfo<>(list);
        list.stream().forEach(item -> {
            item.setTitle(msgTitleMap.get(item.getMsgId()));
            item.setUploaderName(accountMap.get(item.getUploaderName()));
            item.setUploadTmStr(DateUtil.getStringDateShort(item.getUploadTm()));
            item.setPrescriptionPicUrl(AliCloudOssUtil.getOriginImgUrl(null, item.getPrescriptionPicUrl()));
        });

        return new PageResult(info, list);
    }


    @Override
    @Transactional(readOnly = true)
    public PageResult selectAuditList(HashMap paramMap) {
        //查询ma_invs_violation_message，获取title
        List<MaInvsViolationMessageEntity> msgList = maInvsViolationMessageMapper.selectList();
        Map<String, String> msgTitleMap = msgList.stream().collect(Collectors.toMap(MaInvsViolationMessageEntity::getMsgId, MaInvsViolationMessageEntity::getTitle));

        List<AccountModel> accountList = dtInvsManageInstitutionSupervisorMapper.selectAllSupervisors();
        Map<String, String> accountMap = accountList.stream().collect(Collectors.toMap(AccountModel::getLoginUser, AccountModel::getName));


        PageHelper.startPage(Integer.parseInt(paramMap.get("pageNum").toString()), Integer.parseInt(paramMap.get("pageSize").toString()));
        List<ViolationManageModel> list = auditPrescriptionMapper.selectAuditList(paramMap);
        PageInfo<ViolationManageModel> info = new PageInfo<>(list);
        list.stream().forEach(item -> {
            item.setTitle(msgTitleMap.get(item.getMsgId()));
            item.setUploaderName(accountMap.get(item.getUploaderName()));
            item.setUploadTmStr(DateUtil.getStringDateShort(item.getUploadTm()));
            item.setAuditTmStr(DateUtil.getStringDateShort(item.getAuditTm()));
            item.setPrescriptionPicUrl(AliCloudOssUtil.getOriginImgUrl(null, item.getPrescriptionPicUrl()));
        });

        return new PageResult(info, list);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateAuditPrescription(AuditPrescriptionModel auditPrescriptionModel) {
        DtInvsPrescriptionManualAuditEntity auditEntity = new DtInvsPrescriptionManualAuditEntity();

        BeanUtils.copyProperties(auditPrescriptionModel, auditEntity);

        Example example = new Example(DtInvsPrescriptionManualAuditEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("medicalId", auditPrescriptionModel.getMedicalId());
        criteria.andEqualTo("institutionId", auditPrescriptionModel.getInstitutionId());
        criteria.andEqualTo("prescriptionId", auditPrescriptionModel.getPrescriptionId());
        auditPrescriptionMapper.updateByExampleSelective(auditEntity, example);
    }


    @Override
    @Transactional(readOnly = true)
    public Integer getManualAuditNum(String invsInstitutionId, List<String> institutionIds) {
        Example example = new Example(DtInvsPrescriptionManualAuditEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("auditResult", 0);
        criteria.andEqualTo("autoAudit", 2);
        criteria.andIn("institutionId", institutionIds);
        int num = auditPrescriptionMapper.selectCountByExample(example);
        return num;
    }

    @Override
    @Transactional(readOnly = true)
    public PageResult selectTemplateList(HashMap paramMap) {
        PageHelper.startPage(Integer.parseInt(paramMap.get("pageNum").toString()), Integer.parseInt(paramMap.get("pageSize").toString()));
        List<TemplateModel> templateModels = auditPrescriptionMapper.selectTemplateList(paramMap);
        PageInfo<TemplateModel> info = new PageInfo<>(templateModels);
        templateModels.stream().forEach(item -> {
            item.setUploadTmStr(DateUtil.getStringDateShort(item.getUploadTm()));
            item.setTemplatePicUrl(AliCloudOssUtil.getOriginImgUrl(null, item.getTemplatePicUrl()));
        });
        return new PageResult(info, templateModels);
    }

    @Override
    public void updateTemplateSign(String institutionId, String templateSign) {
        Example example = new Example(DtPrescriptionTemplateManageEntity.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("institutionId", institutionId);
        DtPrescriptionTemplateManageEntity entity = new DtPrescriptionTemplateManageEntity();
        entity.setTemplateSign(templateSign);
        dtPrescriptionTemplateManageMapper.updateByExampleSelective(entity, example);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAuditPrescription(Integer reviewedId) {
        auditPrescriptionMapper.deleteByPrimaryKey(reviewedId);
    }
}